package com.lepu.app.fun.tool.bbwy;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.ui.wheel.WheelView;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class BbwyMainSleepActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, AsyncRequest {

	private static final String REQUEST_FINISH = "request_finish";

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bbwy_activity_sleep);

		UtilityBase.setStatusBarRes(this, R.color.color_bbwy, true);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("睡觉");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setTopbarBackground(R.color.color_bbwy);

	}

	public void viewClick(View view) {
		int rid = view.getId();

		switch (rid) {
		case R.id.fallSleepTimeTextView:

			showFallOrWakeTime(0);
			break;
		case R.id.wakeUpTimeTextView:

			showFallOrWakeTime(1);
			break;
		case R.id.finishButton:

			submit();
			break;

		default:
			break;
		}
	}

	private void submit() {

		String FallAsleepTime = "";
		String WakeUpTime = "";

		TextView fallSleepTimeTextView = (TextView) findViewById(R.id.fallSleepTimeTextView);
		TextView wakeUpTimeTextView = (TextView) findViewById(R.id.wakeUpTimeTextView);

		FallAsleepTime = fallSleepTimeTextView.getText().toString();
		WakeUpTime = wakeUpTimeTextView.getText().toString();

		if (FallAsleepTime.equals("请选择") && WakeUpTime.equals("请选择")) {
			if (FallAsleepTime.equals("请选择")) {
				UIHelper.showToast(this, R.string.bbwy_sleep_tip_1);
				return;
			}

			if (WakeUpTime.equals("请选择")) {
				UIHelper.showToast(this, R.string.bbwy_sleep_tip_2);
				return;
			}
		}
		
		if (FallAsleepTime.equals("请选择")) {
			FallAsleepTime = "";
		}

		if (WakeUpTime.equals("请选择")) {
			WakeUpTime = "";
		}
		
		if(!TextUtils.isEmpty(FallAsleepTime) && !TextUtils.isEmpty(WakeUpTime))
		{
			Date fallDate = DateUtilBase.dateFromString(FallAsleepTime,
					DateUtilBase.DATE_ALL);
			Date wakeDate = DateUtilBase.dateFromString(WakeUpTime,
					DateUtilBase.DATE_ALL);

			long fallTime = fallDate.getTime();
			long wakeTime = wakeDate.getTime();

			if (wakeTime <= fallTime) {
				UIHelper.showToast(this, R.string.bbwy_sleep_tip_4);
				return;
			}
		}
		

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());

			params.put("RecordType", 3);

			params.put("FallAsleepTime", FallAsleepTime);
			params.put("WakeUpTime", WakeUpTime);

		} catch (Exception e) {
		}
		map.put("ht", params);

		ApiClient.http_post_main(Setting.getSaveBabyFeedingRecordUrl(), map,
				null, this, REQUEST_FINISH, true);
	}

	private void showFallOrWakeTime(final int type) {
		
		String title = "";
		if(type == 0)
		{
			title = "请选择睡着时间";
		}else{
			title = "请选择睡醒时间";
		}
		new MyDialog(this)
				.setTitle(title)
				.setDateFullTimePicker(new Date(System.currentTimeMillis()))
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						// 如果是个数,则显示为"02"的样式
						WheelView wv_year = (WheelView) v
								.findViewById(R.id.yearFull);
						WheelView wv_month = (WheelView) v
								.findViewById(R.id.monthFull);
						WheelView wv_day = (WheelView) v
								.findViewById(R.id.dayFull);

						WheelView wv_hour = (WheelView) v
								.findViewById(R.id.hourFull);
						WheelView wv_minute = (WheelView) v
								.findViewById(R.id.minuteFull);

						String parten = "00";
						DecimalFormat decimal = new DecimalFormat(parten);
						// 设置日期的显示
						String text = (wv_year.getCurrentItem() + MyDialog.START_YEAR)
								+ "-"
								+ decimal.format((wv_month.getCurrentItem() + 1))
								+ "-"
								+ decimal.format((wv_day.getCurrentItem() + 1));
						String time = decimal.format((wv_hour.getCurrentItem()))
								+ ":"
								+ decimal.format((wv_minute.getCurrentItem()));

						String date = text + " " + time;

						Date selectDate = DateUtilBase.dateFromString(date,
								DateUtilBase.DATE_ALL);
						long selectTime = selectDate.getTime();
						Date currentDate = DateUtilBase.dateFromString(
								DateUtilBase.stringFromDate(new Date(),
										DateUtilBase.DATE_ALL),
								DateUtilBase.DATE_ALL);
						long currentTime = currentDate.getTime();

						if (selectTime > currentTime) {
							UIHelper.showToast(BbwyMainSleepActivity.this,
									R.string.bbwy_sleep_tip_3);
							return;
						}

						if (type == 0) {
							TextView fallSleepTimeTextView = (TextView) findViewById(R.id.fallSleepTimeTextView);
							fallSleepTimeTextView.setText(date);
						} else if (type == 1) {
							TextView wakeUpTimeTextView = (TextView) findViewById(R.id.wakeUpTimeTextView);
							wakeUpTimeTextView.setText(date);
						}

					}
				}).create(null).show();
	}

	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_FINISH)) {
			hideProgressDialog();

			finish(true);

			BbwyMainActivity instance = (BbwyMainActivity) AppManager
					.getAppManager().getActivity(BbwyMainActivity.class);
			if (instance != null) {
				instance.request(false);
			}
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		if (object.equals(REQUEST_FINISH)) {
			hideProgressDialog();
		}
	}

}
